package com.yhp.web;

import com.yhp.bo.OrderItemBO;
import com.yhp.enums.OrderStatusEnum;
import com.yhp.enums.PayMethod;
import com.yhp.pojo.OrderStatus;
import com.yhp.service.OrderService;
import com.yhp.utils.CookieUtils;
import com.yhp.utils.JSONResult;
import com.yhp.utils.RedisOperator;
import com.yhp.vo.MerchantOrdersVO;
import com.yhp.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "订单请求处理类",tags = {"订单请求处理类"})
@RestController
@RequestMapping("/orders")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @Autowired  //需要添加配置类 WebMvcConfig.java
    private RestTemplate restTemplate;

    @Autowired
    private RedisOperator redisOperator;

    //创建订单
    @PostMapping("/create")
    @ApiOperation(value = "创建订单",notes = "创建订单",httpMethod = "POST")
    public JSONResult createOrder(@RequestBody OrderItemBO orderItemBO, HttpServletRequest request, HttpServletResponse response){
        if(orderItemBO.getPayMethod()!= PayMethod.WEIXIN.type&&
          orderItemBO.getPayMethod()!=PayMethod.ALIPAY.type){
            return  JSONResult.errorMsg("支付方式不正确");
        }

        System.out.println("订单信息:"+orderItemBO);
        //1.创建订单
        OrderVO orderVO = orderService.createOrder(orderItemBO);
        String orderid = orderVO.getOrderId();
        //2.创建订单后，移除购物车中已结算(已提交)的商品
        //TODO 整合redis之后，完善购物车中的已结算商品清除，并同步到前端的cookie









        //暂时情况所有数据，实际上应该清除结算的商品
        CookieUtils.setCookie(request,response,FOODIE_SHOPCART,"");
        //3.向支付中心发送当前订单，用于保存支付中心的订单数据-后台直接发送请求到支付中心
        //测试环境中，这里设置支付金额为1分钱
        MerchantOrdersVO merchantOrdersVO = orderVO.getMerchantOrdersVO();
        merchantOrdersVO.setReturnUrl(PAY_RETURN_URL);
        merchantOrdersVO.setAmount(1);


        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("imoocUserId","158047-594226632");
        headers.add("password","ru89-034u-t895-ut8t");

        HttpEntity<MerchantOrdersVO> entity = new HttpEntity<>(orderVO.getMerchantOrdersVO(), headers);

        ResponseEntity<JSONResult> responseEntity =
                restTemplate.postForEntity(PAYMENTSERVERURL, entity, JSONResult.class);

        JSONResult paymentResult = responseEntity.getBody();
        if(paymentResult.getStatus()!=200){
            return JSONResult.errorMsg("支付中心订单创建失败，请联系管理员");
        }
        return JSONResult.ok(orderid);
    }

    //支付异步通知接口
    //http://localhost:8088/orders/notifyOrderPaid   支付异步通知url
    @PostMapping("/notifyOrderPaid")
    public Integer notifyOrderPaid(String merchantOrderId){
            orderService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
            return HttpStatus.OK.value();
    }

    //支付过后，前端发送订单状态的轮询请求
    @PostMapping("/getPaidOrderInfo")
    public JSONResult getOrderStatus(String orderId){
        if(StringUtils.isBlank(orderId)){
            return JSONResult.errorMsg("订单id不允许为空");
        }
        OrderStatus orderStatus = orderService.getOrderStatus(orderId);
        return JSONResult.ok(orderStatus);
    }

}
